package com.ruoyi.station.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.station.domain.*;
import com.ruoyi.station.mapper.SDeviceAddPropertiesMapper;
import com.ruoyi.station.mapper.SDevicePropertiesMapper;
import com.ruoyi.station.mapper.SEquipmentMapper;
import com.ruoyi.station.mapper.SMachineCabinetMapper;
import com.ruoyi.station.service.SEquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-06
 */
@Slf4j
@Service
public class SEquipmentServiceImpl extends ServiceImpl<SEquipmentMapper, SEquipment> implements SEquipmentService {
    @Autowired
    private SEquipmentMapper sEquipmentMapper;
    @Autowired
    private SMachineCabinetMapper sMachineCabinetMapper;
    @Autowired
    private SDevicePropertiesMapper sDevicePropertiesMapper;
    @Autowired
    private SDeviceAddPropertiesMapper sDeviceAddPropertiesMapper;
    /**
     * 导入设备数据
     * @param file
     * @return
     */
    @Override
    @Transactional
    public String importsSEquipment(MultipartFile file) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<List<Object>> lists = FileUtils.parseUploadFile(file);
        int i = 3;
        for (List<Object> readEquipment : lists) {
            //设备ID
            String equipmentId = String.valueOf(readEquipment.get(1)).trim();
            if(StringUtils.isBlank(equipmentId)) return "第" + i +"行设备ID不正确！";
            //所属系统
            String system = String.valueOf(readEquipment.get(2)).trim();
            if(StringUtils.isBlank(system)) return "第" + i +"行所属系统不正确！";
            //所属机柜
            String machineCabinetName = String.valueOf(readEquipment.get(3)).trim();
            SMachineCabinet sMachineCabinet = sMachineCabinetMapper.selectOne(
                    new LambdaQueryWrapper<SMachineCabinet>().eq(SMachineCabinet::getMachineCabinetName, machineCabinetName).eq(SMachineCabinet::getCreateBy,user.getUserName()));
            if (sMachineCabinet == null ) return "第" + i +"行所属机柜不存在！";
            //设备类型
            String equipmentType = DictUtils.getDictValue("equipment_type", String.valueOf(readEquipment.get(4)).trim());
            if(StringUtils.isBlank(equipmentType)) return "第" + i +"行设备类型不存在！";
            //设备型号
            String equipmentModel = String.valueOf(readEquipment.get(5)).trim();
            if(StringUtils.isBlank(equipmentModel)) return "第" + i +"行设备型号不正确！";
            //运行状态
            String runningStatus = DictUtils.getDictValue("running_status", String.valueOf(readEquipment.get(6)).trim());
            if(StringUtils.isBlank(runningStatus)) return "第" + i +"行运行状态不存在！";
            //设备其他属性
            String equipmentProperties = String.valueOf(readEquipment.get(7)).trim();
            String[] propertiesSplit = equipmentProperties.split(",");

            SEquipment equipmenBuild = SEquipment.builder()
                    .equipmentId(equipmentId)
                    .system(system)
                    .machineCabinet(sMachineCabinet.getId())
                    .equipmentType(equipmentType)
                    .equipmentModel(equipmentModel)
                    .runningStatus(runningStatus)
                    .fillBy(user.getUserName())
                    .fillById(String.valueOf(user.getUserId()))
                    .fillDept(user.getDept().getDeptName())
                    .fillDeptId(user.getDeptId())
                    .createBy(user.getUserName())
                    .createTime(DateUtil.date())
                    .build();
            sEquipmentMapper.insert(equipmenBuild);//新增设备

            for (String properties : propertiesSplit) {
                String propertiesName = properties.split(":")[0];
                String propertiesValue = properties.split(":")[1];
                SDeviceProperties sDeviceProperties = sDevicePropertiesMapper.selectOne(new LambdaQueryWrapper<SDeviceProperties>()
                        .eq(SDeviceProperties::getDeviceName, propertiesName)
                        .eq(SDeviceProperties::getCreateBy, user.getUserName()));
                if (sDeviceProperties == null ) return "第" + i +"行"+propertiesName+"属性不存在！";
                SDeviceAddProperties sDeviceAddPropertiesBuild = SDeviceAddProperties.builder()
                        .equipmentIdKey(equipmentId)
                        .deviceName(propertiesName)
                        .deviceCondition(sDeviceProperties.getDeviceCondition())
                        .unit(sDeviceProperties.getUnit())
                        .value(propertiesValue)
                        .dataSource(sDeviceProperties.getDataSource())
                        .createBy(user.getUserName())
                        .createTime(DateUtil.date())
                        .build();
                sDeviceAddPropertiesMapper.insert(sDeviceAddPropertiesBuild);//新增设备属性
            }
        }
        return "成功导入"+lists.size()+"条";
    }
}
